package com.tenement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.common.GlobalException;
import com.tenement.dto.SelectDto;
import com.tenement.entity.House;
import com.tenement.enums.RespBusiness;
import com.tenement.mapper.HouseMapper;
import com.tenement.service.HouseService;
import com.tenement.service.HouseUserService;
import com.tenement.vo.QueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tenement.entity.table.HouseTableDef.HOUSE;
import static com.tenement.entity.table.HouseUserTableDef.HOUSE_USER;
import static com.tenement.entity.table.UserTableDef.USER;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    private final HouseMapper houseMapper;

    private final HouseUserService houseUserService;

    @Override
    public Page<House> list(QueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(HOUSE)
                .leftJoin(HOUSE_USER).on(HOUSE_USER.HOUSE_ID.eq(HOUSE.ID))
                .leftJoin(USER).on(USER.ID.eq(HOUSE_USER.USER_ID))
                .select(
                  HOUSE.ID, HOUSE.AREA, HOUSE.FLOOR, HOUSE.STATUS,
                  HOUSE.REMARKS, HOUSE.BUILDING, HOUSE.ROOM_TYPE,
                  HOUSE.FLOOR_NAME, HOUSE.CREATE_TIME,HOUSE.MOVE_IN_DATE,
                  HOUSE.PROPERTY_ID, HOUSE.UNIT_NUMBER, HOUSE.UPDATE_TIME,
                  HOUSE.PURCHASE_DATE,HOUSE.GAS_METER_NUMBER,HOUSE.LAST_RENOVATION,
                  HOUSE.WATER_METER_NUMBER,HOUSE.ELECTRICITY_METER_NUMBER,
                  HOUSE_USER.STATUS, HOUSE_USER.REMARK,HOUSE_USER.HOUSE_ID,HOUSE_USER.USER_ID,
                  USER.REAL_NAME,USER.NAME,USER.ID_CARD,USER.PHONE_NUMBER ,USER.SEX,
                  USER.PROFILE_PICTURE,USER.TYPE,USER.ID
                )
                .where(
                        HOUSE.FLOOR_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))
                                .or(HOUSE.UNIT_NUMBER.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(HOUSE.REMARKS.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.REAL_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.PHONE_NUMBER.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.ID_CARD.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                ).and(HOUSE.STATUS.eq(queryVo.getStatus(), queryVo.getStatus() != -1))
                .orderBy(HOUSE.CREATE_TIME,false)
                .orderBy(HOUSE.UPDATE_TIME,false);
        return houseMapper.paginate(queryVo.getPage(), queryVo.getSize(),queryWrapper);
    }

    @Override
    public int remove(List<Long> ids) {
        long exists = houseUserService.exists(ids);
        if (exists > 0) {
            throw new GlobalException(RespBusiness.HOUSE_EXISTING_USER);
        }
        return houseMapper.deleteByQuery(QueryWrapper.create().from(HOUSE).where(HOUSE.ID.in(ids)));
    }

    @Override
    public Map<String, Object> statusPieChart() {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(QueryMethods.case_()
                                .when(HOUSE.STATUS.eq(0)).then("空闲")
                                .when(HOUSE.STATUS.eq(1)).then("已售")
                                .when(HOUSE.STATUS.eq(2)).then("自住")
                                .when(HOUSE.STATUS.eq(3)).then("租易")
                                .end().as("label")
                        , QueryMethods.count(HOUSE.ID).as("value"))
                .from(HOUSE)
                .groupBy(HOUSE.STATUS);
        List<SelectDto> pieData = houseMapper.selectListByQueryAs(queryWrapper, SelectDto.class);
        Map<String, Object> res = new HashMap<>();
        res.put("pieData", pieData);
        res.put("total", this.count());
        return res;
    }


}
